$(function () {
    $('.personal_b').click(function () {
        $('.user_detail').children().hide();
        $('.personal').show();
    });

    $('.article_b').click(function () {
        $('.user_detail').children().hide();
        $('.article').show();
    });

    $('.collect_b').click(function () {
        $('.user_detail').children().hide();
        $('.collect').show();
    });

    $('.fans_b').click(function () {
        $('.user_detail').children().hide();
        $('.fans').show();
    });

    $('.focus_b').click(function () {
        $('.user_detail').children().hide();
        $('.focus').show();
    });

    $('.change_detail').click(function () {
        $('.user_detail_show').css({'display': 'none'});
        $('.user_detail_change').css({'display': 'block'});
    });

    $('.personal').submit(function (e) {
        e.preventDefault();
        // location.reload();
        var avatar_url = $('.avatar_url').val();
        var nick_name = $('.nick_name').val();
        var sign = $('.sign').val();
        var phone = $('.phone').val();
        var sex = $('.sex:checked').val();
        var params = {
            "avatar_url": avatar_url,
            "nick_name": nick_name,
            "sign": sign,
            "phone": phone,
            "sex": sex
        };
        $.ajax({
            url: "/user/save_detail",
            type: "post",
            data: JSON.stringify(params),
            contentType: "application/json",
            success: function (resp) {
                if (resp.err == "0") {
                    location.reload();
                } else {
                    alert(resp.errmsg)
                }
            }
        })
    });
});

function delete_article(obj) {
    var art_id = $(obj).attr('value');
    $.ajax({
        url: "/user/art_delete/" + art_id,
        method: "get",
        success: function (resp) {
            if (resp.err == "0") {
                // 刷新界面
                alert("删除成功，即将为您刷新页面");
                location.reload();
            } else {
                alert(resp.errmsg);
            }
        }
    })
}

function delete_classify(obj) {
    var cls_id = $(obj).attr('value');
    $.ajax({
        url: "/user/cls_delete/" + cls_id,
        method: "get",
        success: function (resp) {
            if (resp.err == "0") {
                // 刷新界面
                alert("删除成功，即将为您刷新页面");
                location.reload();
            } else {
                alert(resp.errmsg);
            }
        }
    })
}

function show_box(obj) {
    var cls_id = $(obj).attr('value');
    $('.change_classify_a').html('');
    if (cls_id) {
        $('.change_classify_a').prepend('<input class="cls_name" type="text" placeholder="请输入分类名" id=' + cls_id + '>')
    } else {
        $('.change_classify_a').prepend('<input class="cls_name" type="text" placeholder="请输入分类名" >')
    }
    $('.change_classify').show()
}

function close_box() {
    $('.change_classify').hide()
}

function save_classify() {
    var cls_id = $('.cls_name').attr('id');
    var cls_name = $('.cls_name').val();
    var params = {'cls_name': cls_name};
    if (cls_id) {
        $.ajax({
            url: "/user/change_cls/" + cls_id,
            method: "post",
            data: JSON.stringify(params),
            contentType: "application/json",
            success: function (resp) {
                if (resp.err == "0") {
                    // 刷新界面
                    alert("修改成功，即将为您刷新页面");
                    location.reload();
                } else {
                    alert(resp.errmsg);
                }
            }
        })
    } else {
        $.ajax({
            url: "/user/new_cls",
            method: "post",
            data: JSON.stringify(params),
            contentType: "application/json",
            success: function (resp) {
                if (resp.err == "0") {
                    // 刷新界面
                    alert("添加成功，即将为您刷新页面");
                    location.reload();
                } else {
                    alert(resp.errmsg);
                    get_auth_code()
                }
            }
        })
    }
}